import java.util.Arrays;

public class Test {
    public static boolean findThreeNum(int[] arr) {
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] % 2 != 0) {
                count++;
                if(count == 3) {
                    return true;
                }
            } else {
                count = 0;
            }
        }
        return false;
    }
    public static void main(String[] args) {
        int[] arr = {1, 2, 34, 3, 4, 5, 7, 23, 12};
        System.out.println(findThreeNum(arr));
    }
    //摩尔投票法
    public static int findNum(int[] array) {
        int count = 0;
        int tmp = array[0];
        for (int i = 0; i < array.length; i++) {
            if(count == 0) {
                tmp = array[i];
            }
            if(tmp == array[i]) {
                count++;
            } else {
                count--;
            }
        }
        return tmp;
    }

    public static void main6(String[] args) {
        int[] array = {4,1,2,1,2,2};
        int ret = findNum(array);
        System.out.println(ret);
    }

    //中间元素法
    public static void main7(String[] args) {
        int[] array = {4,1,2,1,2,2};
        Arrays.sort(array);
        System.out.println("多数元素为: " + array[array.length / 2] );
    }

    public static int[] numAdd(int[] nums, int target) {
        int[] ret = {-1};
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if(nums[i] + nums[j] == target) {
                    return new int[]{i, j};
                }
            }
        }
        return ret;//没找到
    }
    public static void main5(String[] args) {
        int[] nums = {2,7,11,15};
        int target = 9;
        System.out.println(Arrays.toString(numAdd(nums, target)));
    }


    public static void bubbleSort(int[] array) {
        //n个数字比较n-1趟
        for (int i = 0; i < array.length - 1; i++) {
            int flag = -1;
            //交换次数
            for (int j = 0; j < array.length - 1 - i; j++) {
                if (array[j] > array[j + 1]) {
                    int tmp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = tmp;
                    flag = 1;
                }
            }
            if (flag == -1) {
                break;
            }
        }
    }
    public static void main4(String[] args) {
        int[] array = {5,3,9,2,7};
        bubbleSort(array);
        System.out.println(Arrays.toString(array));
    }


    public static void select(int[] array) {
        int i = 0;
        int j = array.length - 1;
        while(i < j) {
            while(i < j && array[i] % 2 == 0) {
                i++;
            }
            while(i < j && array[j] % 2 != 0) {
                j--;
            }
            int tmp = array[i];
            array[i] = array[j];
            array[j] = tmp;
        }
    }

    public static void main3(String[] args) {
        int[] array = {1,2,3,4,5,6};
        select(array);
        System.out.println(Arrays.toString(array));
    }
    public static int avg(int[] array) {
        int ret = 0;
        for (int i = 0; i < array.length; i++) {
            ret += array[i];
        }
        ret = ret / array.length;
        return ret;
    }
    public static void main2(String[] args) {
        int[] array = new int[]{1,2,3,4,5};
        int ret = avg(array);
        System.out.println(ret);
    }
    public static int[] transfrom(int[] array) {
        int[] ret = array;
        for (int i = 0; i < array.length; i++) {
            ret[i] = 2 * ret[i];
        }
        return ret;
    }
    public static void main1(String[] args) {
        int[] array = {1, 2, 3};
        System.out.println(Arrays.toString(transfrom(array)));
    }
}
